//
//  ShippingAddressController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/4.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "ShippingAddressController.h"
#import "ShippingAddressCell.h"
#import "AddAddressController.h"
#import "EditAddressController.h"

@interface ShippingAddressController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation ShippingAddressController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    [self requestAddressListData];
}
- (void)setupUI{
    self.title = @"收货地址";
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
    [self.rightbutton setImage:imgname(@"添加") forState:UIControlStateNormal];
    [self.rightbutton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    self.tableView.rowHeight = 110;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
}
- (void)requestAddressListData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    [NetWorkTool POST:getUserAddress param:param success:^(id dic) {
        self.dataArray = [ShippingAddressModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.tableView reloadData];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShippingAddressModel *model = self.dataArray[indexPath.row];
    ShippingAddressCell *cell = [ShippingAddressCell cellWithTableView:tableView];
    cell.model = model;
    [cell.editAddressButton addTarget:self action:@selector(editAddressButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.editAddressButton.tag = indexPath.row;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.fromMineController == YES) {
        
    } else {
        ShippingAddressModel *model = self.dataArray[indexPath.row];
        if (self.selectedAddressClick) {
            self.selectedAddressClick(model);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
// 添加
- (void)rightButtonAction:(UIButton *)button{
    AddAddressController * vc = [[AddAddressController alloc] init];
    vc.addSuccessfulClick = ^{
        [self requestAddressListData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)editAddressButtonClick:(UIButton *)button{
    ShippingAddressModel *model = self.dataArray[button.tag];
    EditAddressController * vc = [[EditAddressController alloc] init];
    vc.model = model;
    vc.editSuccessfulClick = ^{
        [self requestAddressListData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
@end
